import {Injectable} from '@angular/core';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';
import {AjaxService} from '../../public/service/ajax.service';
import {SettingUrl} from '../../public/setting/setting_url';
import {HttpCodesEnum} from '../../public/setting/enums';

@Injectable({
  providedIn: 'root'
})
export class NewsService {

  constructor(private notification: NzNotificationService,
              private ajaxService: AjaxService,
              private message: NzMessageService) {
  }

  /**
   * 查询News列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getNewsList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.news.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 添加News
   * @param params
   * @returns {Promise<T>}
   */
  addNews(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.post({
        url: SettingUrl.URL.news.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 修改News
   * @param params
   * @returns {Promise<T>}
   */
  modifyNews(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.news.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 修改News状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyNewsState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.news.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 查询News详细信息
   * @param code News编码
   * @returns {Promise<T>}
   */
  loadNewsByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.news.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 删除News
   * @param code News编码
   * @returns {Promise<T>}
   */
  deleteNews(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.del({
        url: SettingUrl.URL.news.delete,
        data: {code: code},
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 查询NewsContent列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getNewsContentList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.newsContent.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 添加NewsContent
   * @param params
   * @returns {Promise<T>}
   */
  addNewsContent(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.post({
        url: SettingUrl.URL.newsContent.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 修改NewsContent
   * @param params
   * @returns {Promise<T>}
   */
  modifyNewsContent(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.newsContent.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 修改NewsContent状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyNewsContentState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.newsContent.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 查询NewsContent详细信息
   * @param code NewsContent编码
   * @returns {Promise<T>}
   */
  loadNewsContentByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.newsContent.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 查询NewsImages列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getNewsImagesList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.newsImages.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 添加NewsImages
   * @param params
   * @returns {Promise<T>}
   */
  addNewsImages(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.post({
        url: SettingUrl.URL.newsImages.add,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 修改NewsImages
   * @param params
   * @returns {Promise<T>}
   */
  modifyNewsImages(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.newsImages.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 修改NewsImages状态
   * @param code 供应商编码
   * @param state 供应商状态
   * @returns {Promise<T>}
   */
  modifyNewsImagesState(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.newsImages.updateState,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 查询NewsImages详细信息
   * @param code NewsImages编码
   * @returns {Promise<T>}
   */
  loadNewsImagesByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.newsImages.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

}
